import { useCallback, useMemo, useState } from 'react';
import { Tooltip, Button } from 'antd';
import { getCurrentUser } from '@/utils/user';
import { commonText } from '@/utils/utils';
import type { GridManager } from 'gantd/lib/grid';
import { checkAuth, getExtraParams, TransferPersonAuths } from './utils';
import { transferOwner, transferInterfacePerson, transferHelpPersons, transferMbomPersons } from './services';
import { staticChangeRow } from '@/pages/xbommanage/SpecialOrder/utils';
import UserSelectorModalView from '@/components/selectors/userselector/SelectorModal';
// import { stateEnum } from '@/pages/xbommanage/SpecialOrder/page/XbomObjectCreate/util';

export interface TransferPersonBtnProps {
  selectedRows: any[]; // 选中 BOM行
  gridManagerRef: { current: GridManager };
  api: string;
  multiple?: boolean; // 是否支持多选
  [key: string]: any;
}

/**
 * 转义责任人按钮组件
 */
export default function TransferPersonBtn(props: TransferPersonBtnProps) {
  const {
    selectedRows = [],
    gridManagerRef,
    type, // 中文按钮类型
    multiple = false, // 人是否可以多选
  } = props;
  //获取当前登陆角色
  const isAdmin = getCurrentUser()?.userLoginName === 'admin';
  // const hasReceived = +selectedRows[0]?.state !== stateEnum.notReceived;
  const [visible, setVisible] = useState(false);
  const [operateLoading, setOperateLoading] = useState(false);

  // 按钮状态设置和文字提示
  const { btnTip, btnable } = useMemo(() => {
    if (selectedRows?.length) {
      // if (!hasReceived) {
      //   // 已经接收订单才可以设置相关责任人操作
      //   return {
      //     btnTip: '请先接收订单',
      //     btnable: false,
      //   };
      // }
      if (isAdmin) {
        // 管理员可以任意操作
        return {
          btnTip: TransferPersonAuths[type]?.title,
          btnable: true,
        };
      }
      return checkAuth(type, selectedRows[0]);
    } else {
      // 列表未选中按钮文案
      return {
        btnTip: commonText.noSelections,
        btnable: false,
      };
    }
  }, [selectedRows, type, isAdmin]);

  // 转移，设置人操作
  const requestEffects = useCallback(
    async (ids) => {
      const params = getExtraParams(type, selectedRows[0], ids);
      switch (type) {
        case 'responsible':
          await transferOwner(params);
          break;
        case 'interface':
          await transferInterfacePerson(params);
          break;
        case 'assist':
          await transferHelpPersons(params);
          break;
        case 'setmbom':
          await transferMbomPersons(params);
          break;
        default:
          break;
      }
    },
    [selectedRows, type],
  );

  // 转移操作
  const doOperate = useCallback(
    async (ids: any[]) => {
      setOperateLoading(true);
      await requestEffects(ids);
      setVisible(false);
      // 静态改变选中行的数据
      staticChangeRow({
        gridManagerRef,
        selectedRows,
        changeObj: {
          [TransferPersonAuths[type]?.fieldName]: Array.isArray(ids) ? ids.join(',') : ids,
        },
      });
      setOperateLoading(false);
    },
    [selectedRows, gridManagerRef, type, requestEffects],
  );

  return (
    <>
      <Tooltip title={btnTip}>
        <Button
          icon="retweet"
          size="small"
          loading={operateLoading}
          disabled={!btnable}
          onClick={() => setVisible(true)}
        >
          {TransferPersonAuths[type]?.title}
        </Button>
      </Tooltip>
      <UserSelectorModalView
        visible={visible}
        onOk={(ids: number[]) => doOperate(ids)}
        onCancel={() => setVisible(false)}
        isActive
        multiple={multiple}
      />
    </>
  );
}
